<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html class="x-admin-sm">

    <head>
        <meta charset="UTF-8">
        <title>旅游公司</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="renderer" content="webkit|ie-comp|ie-stand">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <meta http-equiv="Cache-Control" content="no-siteapp" />
        <script type="text/javascript" src="./resources/js/jquery-3.4.1.min.js"></script>
        <link rel="stylesheet" href="./resources/css/font.css">
        <link rel="stylesheet" href="./resources/css/xadmin.css">
        <script src="./resources/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./resources/js/xadmin.js"></script>

    </head>
    <script>
        function searchClick(){
            //发异步，把数据提交给servlet
            var companyName = $('#companyName').val();
            console.log("companyName:"+companyName);
            $.ajax({
                type:"POST",
                data:{"method":"companyList","companyName":companyName},
                url:"./companyControl",
                dataType:"json",
                success:function (data) {
                    console.log("success:"+data);
                    outUser(data);
                },
                error:function(data){
                    console.log("error:"+data);
                }
            });
            return false;
        };

        $(document).ready(function () {
            console.log("页面载入");
            var page = 1;
            companyList(page);
        });

        //公司列表 向服务器发送ajax请求
        function companyList(page) {
            $.ajax({
                type: "POST",
                dataType: "json",
                data:{"method":"companyList","page":page},
                url:"./companyControl",
                success:function (data) {
                    outUser(data);
                    console.log(data);
                }
            });
            return false;
        };

        //输出json数据
        function outUser(data) {
            var result = "";
            for(var i = 0;i<data["data"].length;i++){

                result+="<tr>";
                // result+="<td><input type=\"checkbox\" name=\"id\" value="+data["data"][i].spotId+"   lay-skin=\"primary\"> </td>";
                result+="<td>"+data["data"][i].companyId+"</td>";
                result+="<td>"+data["data"][i].companyName+"</td>";
                result+="<td>"+data["data"][i].userId+"</td>";
                result+="<td class='td-manage'>" +
                    "<a title=\"编辑\"  onclick=\"xadmin.open('编辑','./companyControl?method=companyFind&companyId="+data["data"][i].companyId+"',300,200)\" href=\"javascript:;\">"
                    +"<i class=\"layui-icon\">&#xe642;</i>"+"</a>"
                    +"<a title=\"删除\" onclick=\"company_del(this,"+data["data"][i].companyId+","+data["data"][i].userId+")\"  href=\"javascript:;\">"
                    +"<i class=\"layui-icon\">&#xe640;</i>"+"</a>"
                    +"</td>";
                result+="</tr>";
            }
            $("#mytbody").html(result);


            /*分页*/
            //字符串存储html文本
            var pagerL ="";
            //当前页
            var nowPage = data["page"];
            for(var i = 1;i<=data["pages"];i++){
                if(i===nowPage){
                    pagerL+="<span class=\"current\">"+i+"</span>"
                }else {
                    pagerL+="<a class=\"num\" href=\"javascript:companyList("+i+")\">"+i+"</a>";
                }
            }
            $("#pagerList").html("<a class=\"prev\" href=\"javascript:companyList(1)\" >&lt;&lt;</a>"
                +pagerL+
                "<a id=\"lastPage\" class=\"next\" href=\"javascript:companyList("+data["pages"]+")\">&gt;&gt;</a>");

        };

        // 景点-删除
        function company_del(obj,companyId,userId){

            //发异步删除数据
            layer.confirm('确认要删除吗？',function(index){
                $.ajax({
                    type:"POST",
                    url:"./companyControl?method=companyDelete&companyId="+companyId+"&userId="+userId ,
                    dataType:"json",
                    success:function (data) {
                        layer.alert("删除成功",{icon:6},function () {
                            $(obj).parents("tr").remove();
                            layer.msg('已删除!',{icon:1,time:1000});
                            // xadmin.close();
                        });
                    },
                    error:function (data) {
                        layer.alert("删除失败,请检查是否有开设的线路未清空！",{icon:6},function () {
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                    }
                });

            });
        }
    </script>
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="./main">首页</a>
                <a href="">用户信息管理</a>
                <a>
                    <cite>旅游公司列表</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5" method="post">
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="text" id="companyName" name="companyName" placeholder="请输入公司名" autocomplete="off" class="layui-input"></div>
                                <div class="layui-inline layui-show-xs-block">
                                    <%--这里要给button加type=button--%>
                                    <button class="layui-btn" type="button" lay-submit="" lay-filter="search" onclick="searchClick()">
                                        <i class="layui-icon">&#xe615;</i></button>
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <%--这里要给button加type=button--%>
                                    <button class="layui-btn" title="打印查询结果" type="button" lay-submit="" lay-filter="" onclick="window.location.href='./download?method=downSearchCompany'" >
                                        <i class="layui-icon">&#xe67d;</i></button>
                                </div>
                            </form>
                        </div>

                        <div class="layui-card-header">
                            <button class="layui-btn" onclick="xadmin.open('添加旅游公司','./companyControl?method=companyAdd',500,350)"><i class="layui-icon"></i>添加</button>

                            <div class="layui-inline layui-show-xs-block">
                                <%--这里要给button加type=button--%>
                                <button class="layui-btn" title="打印所有公司" type="button" lay-submit="" lay-filter="" onclick="window.location.href='./download?method=downCompany'" >
                                    <i class="layui-icon">&#xe67d;</i></button>
                            </div>


                        </div>

                        <div class="layui-card-body ">
                            <table class="layui-table layui-form"  lay-filter="test">
                                <thead>
                                    <tr>
                                        <th lay-data="{field:'companyId', width:80, sort: true}">ID</th>
                                        <th lay-data="{field:'companyName', width:150, sort: true, edit: 'text'}">公司名</th>
                                        <th lay-data="{field:'userId', edit: 'text', width: 200}">用户ID</th>
                                        <th lay-data="{ width: 200}">操作</th>
                                    </tr>
                                </thead>
                                <tbody id="mytbody"></tbody>
                            </table>
                        </div>
                        <div class="layui-card-body ">
                            <div class="page">
                                <div id="pagerList">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>